package com.vito.dcs.sys.layer.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vito.base.factory.IDFactory;
import com.vito.base.model.DataGrid;
import com.vito.dcs.sys.layer.dao.SysLayerMapper;
import com.vito.dcs.sys.layer.model.SysLayerModel;
import com.vito.dcs.sys.layer.service.SysLayerService;
import com.vito.dcs.sys.material.dao.SysMaterialMapper;

/**
 * 结构层
 * @author lyhao
 *
 */
@Service
public class SysLayerServiceImpl implements SysLayerService {
	
	@Autowired
	private SysLayerMapper sysLayerMapper;
	@Autowired
	private SysMaterialMapper sysMaterialMapper;
	
	@Override
	public void saveSysLayer(SysLayerModel sysLayerModel){
		sysLayerModel.setId(IDFactory.getIDFactory().getIdInc()+"");          
		sysLayerMapper.saveSysLayer(sysLayerModel);
	}
	@Override
	public DataGrid<SysLayerModel> querySysLayerByPage(SysLayerModel sysLayerModel){
		DataGrid<SysLayerModel> dg = new DataGrid<SysLayerModel>();
		dg.setRows(sysLayerMapper.querySysLayerByPage(sysLayerModel));
		dg.setTotal(sysLayerModel.getTotal());
		return dg;
	}
	@Override
	public SysLayerModel querySysLayer(SysLayerModel sysLayerModel){
		return sysLayerMapper.querySysLayer(sysLayerModel);
	}
	@Override
	public SysLayerModel updateSysLayer(SysLayerModel sysLayerModel){
		if(sysLayerMapper.updateSysLayer(sysLayerModel)>0){
			return sysLayerModel;
		}else{
			return null;
		}
	}
	
	@Override
	public Integer deleteSysLayer(SysLayerModel sysLayerModel){
		return sysLayerMapper.deleteSysLayer(sysLayerModel);
	}
	@Override
	public JSONObject queryLayerAndMaterialList() {
		SysLayerModel sysLayerModel = new SysLayerModel();
		List<SysLayerModel> layerList = sysLayerMapper.findLayerList(sysLayerModel);
		JSONArray arr = new JSONArray();
		JSONObject obj = null;
		for(int i=0;i<layerList.size();i++){
			obj = new JSONObject();
			obj.put("layerId", layerList.get(i).getId());
			obj.put("layerName", layerList.get(i).getLayerName());
			obj.put("items", sysMaterialMapper.findMaterialList(layerList.get(i).getId()));
			arr.add(obj);
		}
		JSONObject json = new JSONObject();
		json.put("layerAndMaterial", arr);
		return json;
	}
	@Override
	public List<SysLayerModel> querySysLayerList(SysLayerModel sysLayerModel) {
		return sysLayerMapper.findLayerList(sysLayerModel);
	}

}